package com.zr.daoimpl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.zr.dao.OrderDaoLPN;
import com.zr.model.Distributionscope;
import com.zr.model.Order;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Repository("orDao")
public class OrderDaoImplLPN extends BaseDaOImpl implements OrderDaoLPN{

	@Override
	public int getOrderCount() {
		//System.out.println("1111111111111111111");
		String hql ="select count(*) from Order";
		//String hql = "select count(or.orId) from Order as  or ";
    	Query query = this.getCurrentSession().createQuery(hql);
    	int count = ((Number) query.uniqueResult()).intValue();
		return count;
	}

	@Override
	public JSONArray findOrder(int page, int rows) {
		JSONArray j=new JSONArray();

		String hql ="from Order";
			
		Query query = this.getCurrentSession().createQuery(hql);
		query.setFirstResult((page-1)*rows);
		query.setMaxResults(rows);
			List<Order> or = query.list();
			System.out.println(or.get(0).getOrDeliveryDate());
			System.out.println(or.get(0).getOrId());
			System.out.println(or.get(0).getOrState());
			System.out.println(or.get(0).getOrRemarks());
			System.out.println(or.get(0).getOrSenderName());
			System.out.println(or.get(0).getOrSenderPhone());
			System.out.println(or.get(0).getOrdertype().getOtName());
			for (Order order : or) {
				JSONObject jo = new JSONObject();
				jo.put("orId", order.getOrId());
				jo.put("orDeliveryDate", order.getOrDeliveryDate());
				jo.put("otName", order.getOrdertype().getOtName());
				jo.put("orState", order.getOrState());
				jo.put("orRemarks", order.getOrRemarks());
				jo.put("orSenderName", order.getOrSenderName());
				jo.put("orSenderPhone", order.getOrSenderPhone());
				jo.put("orSenderCompanyPhone", order.getOrSenderCompanyPhone());
				jo.put("orSenderAddr", order.getOrSenderAddr());
				jo.put("orRecipientName", order.getOrRecipientName());
				jo.put("orRecipientPhone", order.getOrRecipientPhone());
				jo.put("orRecipientCompanyPhone", order.getOrRecipientCompanyPhone());
				jo.put("orRecipientAddr", order.getOrRecipientAddr());
				jo.put("orCargoWeight", order.getOrCargoWeight());
				jo.put("orCargoVolume", order.getOrCargoVolume());
				jo.put("orItemName", order.getOrItemName());
				jo.put("orItemNumber", order.getOrItemNumber());
				jo.put("cyName", order.getCargotype().getCyName());
				jo.put("orInsuredValue", order.getOrInsuredValue());
				jo.put("orPostage", order.getOrPostage());
				jo.put("orTotalCost", order.getOrTotalCost());
				jo.put("orPaymentMethod", order.getOrPaymentMethod());
				jo.put("orIssuanceDate", order.getOrIssuanceDate());
				jo.put("dpNamef", order.getDeliverypointByDpIdf().getDpName());
				jo.put("dpNames", order.getDeliverypointByDpIds().getDpName());
				jo.put("csCurrentLocation", order.getCargostatus().getCsCurrentLocation());
				jo.put("customerName", order.getCustomer().getCustomerName());
				jo.put("eiName", order.getEmployeeinformation().getEiName());
				j.add(jo);
				
			}
	
		
		System.out.println(j.get(0).toString());
		return j;
	}

	@Override
	public int updateOR(int orId, String orState) {
		Order or = (Order) this.getCurrentSession().get(Order.class, orId);
		or.setOrState(orState);
		int i = or.getOrId();
		return i;
	}

	@Override
	public JSONArray getOrderByState(int page, int rows, String orState) {
		JSONArray j = new JSONArray();
		String hql = "  from Order ord where ord.orState=:orState";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("orState",orState);
		query.setFirstResult((page-1)*rows);
		query.setMaxResults(rows);
		List<Order> or = query.list();
		for (Order order : or) {
			JSONObject jo = new JSONObject();
			jo.put("orId", order.getOrId());
			jo.put("dpNamef", order.getDeliverypointByDpIdf().getDpName());
			jo.put("dpNames", order.getDeliverypointByDpIds().getDpName());
			jo.put("orState", order.getOrState());
			j.add(jo);
		}
		return j;
	}

	@Override
	public int getOrderCount(String orState) {
		String hql ="select count(*) from Order ord where ord.orState=:orState ";
		
    	Query query = this.getCurrentSession().createQuery(hql);
    	query.setString("orState",orState);
    	int count = ((Number) query.uniqueResult()).intValue();
		return count;
	}

}
